import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:gt_flutter_admin/widgets/image_picker/image_picker_view.dart';
import 'package:gt_flutter_admin/constant/error_msg.dart';
import 'package:gt_flutter_admin/pages/system/user/user_update_controller.dart';
import 'package:gt_flutter_admin/enum/user_sex.dart';

import '../../../constant/regexp.dart';

class UserUpdateView extends GetView<UserUpdateController> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(15),
      child: ListView(children: [Obx(() => _buildForm())]),
    );
  }

  // 构建表单
  Form _buildForm() {
    return Form(
      child: Column(
        children: [
          FormField(
            builder: (field) => BrnTextInputFormItem(
              controller: controller.usernameText,
              title: '用户名',
              hint: '请输入用户名',
              isRequire: true,
              error: field.errorText ?? '',
            ),
            validator: (value) {
              String username = controller.usernameText.text;
              if (username.isEmpty) {
                return ErrorMsgs.NOT_NULL;
              } else if (username.length < 2 || username.length > 20) {
                return ErrorMsgs.STRING_RANGE(2, 20);
              } else {
                return null;
              }
            },
          ),
          // FormField(builder: (context)),
          BrnTextInputFormItem(
            controller: controller.passwordText,
            title: "密码",
            hint: "默认为1234qwer",
          ),
          FormField(
            builder: (field) => BrnTextInputFormItem(
              controller: controller.emailText,
              title: '邮箱',
              hint: '请输入邮箱',
              isRequire: true,
              error: field.errorText ?? '',
            ),
            validator: (value) {
              String email = controller.emailText.text;
              if (email.isEmpty) {
                return ErrorMsgs.NOT_NULL;
              } else if (email.length > 50) {
                return ErrorMsgs.MAX_LENGTH(50);
              } else if (!Regexps.isEmail(email)) {
                return ErrorMsgs.FORMAT_ILLEGAL;
              } else {
                return null;
              }
            },
          ),
          FormField(
            builder: (field) => BrnTextInputFormItem(
              controller: controller.mobileText,
              title: '联系电话',
              hint: '请输入联系电话',
              isRequire: true,
              error: field.errorText ?? '',
            ),
            validator: (value) {
              String mobile = controller.mobileText.text;
              if (mobile.isEmpty) {
                return ErrorMsgs.NOT_NULL;
              } else if (!Regexps.isMobile(mobile)) {
                return ErrorMsgs.FORMAT_ILLEGAL;
              } else {
                return null;
              }
            },
          ),
          BrnBaseTitle(
            title: '状态',
            customActionWidget: Container(
              child: Switch(
                value: controller.statusSwitch.value,
                onChanged: (value) => controller.statusSwitch.value = value,
              ),
            ),
          ),
          BrnRadioInputFormItem(
            title: '性别',
            options: [
              for (int i = 0; i < UserSex.values.length; i++)
                UserSex.values[i].value,
            ],
            value: controller.sexRadio.value,
            onChanged: (oldStr, newStr) {
              controller.sexRadio.value = newStr ?? '';
            },
          ),
          FormField(
            builder: (field) => BrnBaseTitle(
              title: '头像',
              isRequire: true,
              customActionWidget: ImagePicker(
                (imageUrl) => controller.avatar = imageUrl,
                imageUrl: controller.avatar,
              ),
            ),
            validator: (value) => ErrorMsgs.validNull(controller.avatar),
          ),
          FormField(
            builder: (field) => BrnTextInputFormItem(
              controller: controller.descriptionText,
              title: '描述',
              hint: '请输入描述',
              isRequire: true,
              error: field.errorText ?? '',
            ),
            validator: (value) {
              String description = controller.descriptionText.text;
              if (description.isEmpty) {
                return ErrorMsgs.NOT_NULL;
              } else {
                return null;
              }
            },
          ),
          FormField(
            builder: (field) => controller.obx(
              (state) => BrnTextQuickSelectFormItem(
                title: "角色",
                isRequire: true,
                btnsTxt: controller.roleOptions.toList(),
                value: controller.roleSelectedValue.value,
                selectBtnList: controller.roleStatus,
                isBtnsScroll: true,
                hint: '请选择角色',
                error: field.errorText ?? '',
                onBtnSelectChanged: controller.onSelectRole,
              ),
            ),
            validator: (value) => ErrorMsgs.validNull(controller.roleIds),
          ),
          FormField(
            builder: (field) => controller.obx(
              (state) => BrnTextQuickSelectFormItem(
                title: "部门",
                isRequire: true,
                btnsTxt: controller.deptOptions.toList(),
                value: controller.deptSelectedValue.value,
                selectBtnList: List.empty(),
                isBtnsScroll: true,
                hint: '请选择部门',
                error: field.errorText ?? '',
                onBtnSelectChanged: controller.onSelectDept,
              ),
            ),
            validator: (value) => ErrorMsgs.validNull(controller.deptId),
          ),
          FormField(
            builder: (field) => controller.obx(
              (state) => BrnTextQuickSelectFormItem(
                title: "数据权限",
                isRequire: true,
                btnsTxt: controller.dataAuthOptions.toList(),
                value: controller.dataAuthSelectedValue.value,
                selectBtnList: controller.dataAuthStatus,
                isBtnsScroll: true,
                hint: '请选择部门',
                error: field.errorText ?? '',
                onBtnSelectChanged: controller.onSelectDataAuth,
              ),
            ),
            validator: (value) => ErrorMsgs.validNull(controller.deptIds),
          ),
          Builder(
            builder: (context) {
              return BrnBigMainButton(
                title: controller.isEdit.value ? '新增' : '编辑',
                onTap: () {
                  if (Form.of(context).validate()) {
                    controller.onSubmit();
                  }
                },
              );
            },
          ),
        ],
      ),
    );
  }
}
